
var OnTapEvent = require('OnTapEvent');
var OnGridLoaded = require('OnGridLoaded');

cc.Class({
    extends: cc.Component,

    properties: {
        wnode: {
            type: cc.Prefab,
            default: null,
        },
        bnode: {
            type: cc.Prefab,
            default: null,
        },
        swicthColor: {
            type: cc.Button,
            default: null,
        },
        startBtn: {
            type: cc.Button,
            default: null,
        },
        etNumber: {
            type: cc.EditBox,
            default: null,
        },
        etID: {
            type: cc.EditBox,
            default: null,
        },
        nodeColor: {
            type: cc.Label,
            default: null,
        }

    },

    // use this for initialization
    onLoad: function () {
        // this.node.on(cc.Node.EventType.TOUCH_END, function(e){
        //     this.connect();
        // }.bind(this));

        // 保存黑白棋子
        this.nodes = [];
        // 建立链接
        this.connect();
        this.refreshDur = 0;

        // 设置属性的值
        this.node.on(OnGridLoaded.Name, function(e){
            this.dd = e.dd;
            this.wh = e.wh;
        }.bind(this));
        
        // 设置颜色   TRUE  黑色
        this.color = true;
        // 修改颜色
        this.swicthColor.node.on(cc.Node.EventType.TOUCH_END, function (e) {
            if (this.nodeColor.string == '白子') {
                this.nodeColor.string = '黑子';
                this.color = true;
            }
            else {
                this.nodeColor.string = '白子';
                this.color = false;
            }

        }.bind(this));

        // 监听简单棋盘的事件
        this.node.on(OnTapEvent.Name, function (e) {
            this.addNode(e.h, e.v, this.color);
            var msg = '{"h": ' + e.h + ', "v": ' + e.v + '}';
            this.sendMsg(msg);
        }.bind(this));

        // 重新开始
        this.startBtn.node.on(cc.Node.EventType.TOUCH_END, function (e) {
            this.sendMsg('"reset"');
            this.reset();
        }.bind(this));
    },
    reset(){
        // 清空棋子
        var n = null;
        for (i = 0; i < this.nodes.length; i++){
            this.node.removeChild(this.nodes[i], true);
        }
        this.nodes.splice(0, this.nodes.length);
    },
    // 链接服务器
    connect(){
        if (this.ws == null || this.ws.readyState != 1) {
            this.ws = new WebSocket("ws://192.168.3.6:8000/websocket");
            this.ws.onopen = function (event) {
                console.log("Send Text WS was opened.");
            }.bind(this);

            this.ws.onmessage = function (event) {
                console.log("response text msg: " + event.data);
                var json = JSON.parse(event.data);
                if (json.msg == 'reset'){
                    this.reset();
                }
                else{
                    var h = json.msg.h;
                    var v = json.msg.v;
                    var color = !this.color;
                    this.addNode(h, v, color);
                }
            }.bind(this);

            this.ws.onerror = function (event) {
                console.log("Send Text fired an error", event);
            };

            this.ws.onclose = function (event) {
                console.log("WebSocket instance closed.");
            }.bind(this);
        }
    },

    // 添加节点  黑白棋子
    addNode: function (h, v, color) {
        var node = null;
        if (color) {
            node = cc.instantiate(this.bnode);
        }
        else {
            node = cc.instantiate(this.wnode);
        }
        var pp = cc.p(this.dd * h - this.wh * 0.5, this.dd * v - this.wh * 0.5);
        pp = this.node.convertToNodeSpace(pp);
        // console.log(pp.x, pp.y);
        node.setPosition(pp);
        node.width = this.dd * 0.8;
        node.height = node.width;
        this.node.addChild(node);

        this.nodes.push(node);
    },

    // 发送消息
    sendMsg: function (msg) {
        this.connect();
        if (this.ws != null && this.ws.readyState == 1){
            this.ws.send('{"room_id": "' + this.etNumber.string + '", "msg": ' + msg + ', "my_id": "' + this.etID.string + '"}')
        }
        else{
            console.log('连接断开');
        }
    },

    // called every frame
    update: function (dt) {
        this.refreshDur += dt;
        if (this.refreshDur > 0.5){
            this.refreshDur = 0;
            this.connect();
        }
    },
});
